package com.zsxj.xixi.common.utils;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Description
 * @Author mengweibin
 * @Date 2021-04-09 17:16
 */
public class TimeUtils {

    private static final String SIMPLE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";


    public static String formatted(LocalDateTime time) {
        return DateTimeFormatter.ofPattern(SIMPLE_TIME_FORMAT).format(time);
    }

    public static LocalDateTime convertTime(String value) {
        if (StringUtils.isEmpty(value)) {
            return null;
        }
        return value.contains("T") ? tTimeFormatFrom(value) : simpleTimeFormatFrom(value);
    }

    private static LocalDateTime simpleTimeFormatFrom(String value) {
        return LocalDateTime.parse(value, DateTimeFormatter.ofPattern(SIMPLE_TIME_FORMAT));
    }

    private static LocalDateTime tTimeFormatFrom(String value) {
        return LocalDateTime.parse(value, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
    }

    public static Long betweenNowDates(LocalDateTime time) {
        return Duration.between(getZeroPointByDate(time), getZeroPointByDate(LocalDateTime.now())).toDays();
    }

    private static LocalDateTime getZeroPointByDate(LocalDateTime time) {
        return LocalDateTime.of(time.getYear(), time.getMonth(), time.getDayOfMonth(), 0, 0);
    }
}
